package com.tarena.lbs.service.impl;

import com.tarena.lbs.base.exception.ServiceException;
import com.tarena.lbs.base.response.StatusCode;
import com.tarena.lbs.mapper.UserMapper;
import com.tarena.lbs.dto.UserQuery;
import com.tarena.lbs.dto.UserRegParam;
import com.tarena.lbs.dto.UserSelectQuery;
import com.tarena.lbs.entity.User;
import com.tarena.lbs.vo.UserVO;
import com.tarena.lbs.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;


    @Override
    public UserVO login(UserQuery userQuery) {
        UserVO userVO=userMapper.login(userQuery);
        if (userVO==null){
            throw new ServiceException(StatusCode.USERNAME_ERROR);
        }
        if (!userVO.getPassword().equals(userQuery.getPassword())){
            throw new ServiceException(StatusCode.PASSWORD_ERROR);
        }
        log.debug("用户查询结果:{}",userVO);
        return userVO;
    }

    @Override
    public void reg(UserRegParam userRegParam) {
        log.debug("用户注册请求:{}",userRegParam);
        User user=new User();
        BeanUtils.copyProperties(userRegParam,user);
        if (user.getId()==null){
            user.setRegTime(new Date());
            userMapper.reg(user);
        }else {
            userMapper.update(user);
        }


    }

    @Override
    public List<UserVO> select(UserSelectQuery userSelectQuery) {
        log.debug("用户查询参数:{}",userSelectQuery);
        List<UserVO> list=userMapper.select(userSelectQuery);
        return list;
    }
}
